﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Data;
//using System.Drawing.Printing;
//using System.Windows.Forms;
//using System.Drawing;

//namespace Check_Printing_Solution
//{
//    public class PrintEngine
//    {
//        PrintManager manager = new PrintManager();
//        TemplateLayout _template = new TemplateLayout();
//        DataRow _currentrow = null;
//        Bank _currentbank = new Bank();
//        Customer _currentcustomer = new Customer();
//        int chequesprinted = 0;
//        int _chequecount = 0;
//        int _coversprinted = 0;

//        private void Print( TemplateLayout styletemplate, int quantity, string printername )
//        {
//            if ( _template.ChquesPerPage == "0" || _template.ChquesPerPage == "" )
//            {
//                chequesprinted = quantity;
//            }

//            while ( chequesprinted <= quantity )
//            {
//                PrintDocument template = new PrintDocument();
//                template.PrintPage += new PrintPageEventHandler( template_PrintPage );
//                template.PrinterSettings.PrinterName = printername;

//                if ( styletemplate.Landscape )
//                {
//                    template.DefaultPageSettings.Landscape = true;
//                }
//                else
//                {
//                    template.DefaultPageSettings.Landscape = false;
//                }

//                PrintPreviewDialog printpreviewdialog = new PrintPreviewDialog();
//                printpreviewdialog.Document = template;
//                printpreviewdialog.Hide();
//                template.Print();
//            }
//        }

//        void template_PrintPage( object sender, PrintPageEventArgs e )
//        {
//            int padding = 0;

//            for ( int i = 0; i < Convert.ToInt32( _template.ChquesPerPage ); i++ )
//            {
//                string checkno = string.Format( "C{0}C A{1}D{2}A {3}C  {4}", _chequecount, _currentbank.BranchCode, _currentbank.BranchPrefix, _currentrow[1].ToString(), _currentbank.ChequeType );

//                if ( _template.PrintNames )
//                {
//                    e.Graphics.DrawString( _currentrow[5].ToString(), new Font( _template.NameFont, Convert.ToSingle( _template.NameFontSize ), FontStyle.Bold ), Brushes.Black, new Point( Convert.ToInt32( _template.NameX ), Convert.ToInt32( _template.NameY ) + padding ) );
//                }
//                if ( _template.PrintNameAddress )
//                {
//                    e.Graphics.DrawString( _currentrow[6].ToString(), new Font( _template.Address1Font, Convert.ToSingle( _template.Address1FontSize ) ), Brushes.Black, new Point( Convert.ToInt32( _template.Address1X ), Convert.ToInt32( _template.Address1Y ) + padding ) );
//                    e.Graphics.DrawString( _currentrow[7].ToString(), new Font( _template.Address2Font, Convert.ToSingle( _template.Address2FontSize ) ), Brushes.Black, new Point( Convert.ToInt32( _template.Address2X ), Convert.ToInt32( _template.Address2Y ) + padding ) );
//                }
//                if ( _template.PrintChequeNos )
//                {
//                    e.Graphics.DrawString( checkno, new Font( _template.ChequeNumberFont, Convert.ToSingle( _template.ChequeNumberFontSize ) ), Brushes.Black, new Point( Convert.ToInt32( _template.ChequeNumberX ), Convert.ToInt32( _template.ChequeNumberY ) + padding ) );
//                }

//                e.Graphics.DrawString( _chequecount.ToString(), new Font( _template.ChequeCountFont, Convert.ToSingle( _template.ChequeCountFontSize ) ), Brushes.Black, new Point( Convert.ToInt32( _template.ChequeCountX ), Convert.ToInt32( _template.ChequeCountY ) + padding ) );
//                e.Graphics.DrawString( _currentbank.BankAddress, new Font( _template.BankFont, Convert.ToSingle( _template.BankFontSize ) ), Brushes.Black, new Point( Convert.ToInt32( _template.BankX ), Convert.ToInt32( _template.BankY ) + padding ) );

//                padding = padding + Convert.ToInt32( _template.DistanceBetweenCheques );
//                chequesprinted++;
//                _chequecount++;
//            }
//        }

//        private void PrintCover( string printername )
//        {
//            chequesprinted = 0;

//            PrintDocument template = new PrintDocument();
//            template.PrintPage += new PrintPageEventHandler( covertemplate_PrintPage );
//            template.PrinterSettings.PrinterName = printername;

//            PrintPreviewDialog printpreviewdialog = new PrintPreviewDialog();
//            printpreviewdialog.Document = template;
//            template.Print();
//        }

//        void covertemplate_PrintPage( object sender, PrintPageEventArgs e )
//        {
//            int padding = 0;

//            for ( int i = 0; i < Convert.ToInt32( _template.ChquesPerPage ); i++ )
//            {
//                //Name
//                e.Graphics.DrawString( _currentrow[5].ToString(), new Font( _template.CoverNameFont, Convert.ToSingle( _template.CoverNameSize ), FontStyle.Bold ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverNameX ), Convert.ToInt32( _template.CoverNameY ) + padding ) );
//                //Address1
//                e.Graphics.DrawString( _currentrow[6].ToString(), new Font( _template.CoverAddress1Font, Convert.ToSingle( _template.CoverAddress1Size ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverAddress1X ), Convert.ToInt32( _template.CoverAddress1Y ) + padding ) );
//                //Address2
//                e.Graphics.DrawString( _currentrow[7].ToString(), new Font( _template.CoverAddress2Font, Convert.ToSingle( _template.CoverAddress2Size ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverAddress2X ), Convert.ToInt32( _template.CoverAddress2Y ) + padding ) );
//                //Phone
//                e.Graphics.DrawString( _currentrow[19].ToString(), new Font( _template.CoverTelephoneFont, Convert.ToSingle( _template.CoverTelephoneSize ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverTelephoneX ), Convert.ToInt32( _template.CoverTelephoneY ) + padding ) );

//                string checkno = string.Format( "C{0}C A{1}D{2}A {3}C  {4}", _currentbank.BranchSequence, _currentbank.BranchCode, _currentbank.BranchPrefix, _currentrow[1].ToString(), _currentbank.ChequeType );
//                e.Graphics.DrawString( checkno, new Font( _template.CoverCheckNoFont, Convert.ToSingle( _template.CoverCheckNoSize ) ), Brushes.Black, new Point( Convert.ToInt32( _template.CoverCheckNoX ), Convert.ToInt32( _template.CoverCheckNoY ) + padding ) );

//                padding = padding + Convert.ToInt32( _template.DistanceBetweenCheques );
//                _coversprinted++;
//            }
//        }

//        public void InitiatePrint( DataSet ds, string printername, JobLog joblog )
//        {
//            manager.AddJobLog( joblog );
//        }

//        public void InitiatePrint( DataSet ds, string printername )
//        {
//            for ( int i = 0; i < ds.Tables["Customers"].Rows.Count; i++ )
//            {
//                _currentrow = ds.Tables["Customers"].Rows[i];

//                //9 Style#
//                string style = ds.Tables["Customers"].Rows[i][9].ToString();

//                //3 Bank Code
//                string bankcode = "0" + ds.Tables["Customers"].Rows[i][0].ToString();
//                _currentbank = manager.GetBankByBankCode( bankcode );

//                TemplateLayout template = manager.GetTemplateLayout( style, bankcode );
//                _template = template;

//                try
//                {
//                    _chequecount = Convert.ToInt32( ds.Tables["Customers"].Rows[i][11].ToString() );
//                }
//                catch ( Exception ex )
//                {
//                    throw new Exception( "Account #:" + ds.Tables["Customers"].Rows[i][1].ToString() + " threw an error.", ex.InnerException );
//                }

//                int quantity = 0;
//                int pagesperbook = 0;
//                int numbooks = 0;

//                try
//                {
//                    quantity = Convert.ToInt32( ds.Tables["Customers"].Rows[i][12].ToString() );
//                    pagesperbook = Convert.ToInt32( ds.Tables["Customers"].Rows[i][13].ToString() );
//                    numbooks = Convert.ToInt32( ds.Tables["Customers"].Rows[i][4].ToString() );
//                }
//                catch ( Exception ex )
//                {
//                    throw new Exception( "Account #:" + ds.Tables["Customers"].Rows[i][1].ToString() + " threw an error.", ex.InnerException );
//                }

//                for ( int j = 0; j < numbooks; j++ )
//                {
//                    if ( template.PrintCovers )
//                    {
//                        PrintCover( printername );
//                    }
//                    Print( template, pagesperbook, printername );
//                    chequesprinted = 0;
//                    _chequecount = 1;
//                }

//                CreateJobLog( "",
//                    ds.Tables["Customers"].Rows[i][12].ToString(),
//                    ds.Tables["Customers"].Rows[i][11].ToString() + " total cheques",
//                    ds.Tables["Customers"].Rows[i][1].ToString(),
//                    ds.Tables["Customers"].Rows[i][9].ToString()
//                    );
//            }
//        }

//        private void CreateJobLog( string comment, string quantity, string lastchequenumber, string accountnumber, string chequetype )
//        {
//            JobLog joblog = new JobLog();
//            joblog.Comment = comment;
//            joblog.DatePrinted = DateTime.Now;
//            joblog.LastQuantity = quantity;
//            joblog.LastChequeNumber = lastchequenumber;
//            joblog.CustomerId = accountnumber;
//            joblog.ChequeTypeId = chequetype;

//            manager.AddJobLog( joblog );
//        }
//    }
//}
